<?php

namespace App\Http\Controllers\H5\Shop;
define("UID_PAY_PWD_CACHE_TOKEN_KEY", "uid_pay_pwd_cache_token_key_");
define("UID_PAY_PWD_CACHE_TOKEN_TIME", 1800);


use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use Illuminate\Support\Facades\Redis;
use App\Common\Model\ShopUserStatus;
use App\Common\Service\ShopAccountService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use \Log;

class PayController extends Controller
{

    public $accountService;

    /**
     * H5Controller constructor.
     * @param $articleService
     */
    public function __construct(ShopAccountService $accountService)
    {
        $this->accountService = $accountService;
    }



    /**
     * 线上支付宝，微信支付提交
     */
    public function paySubmit(Request $request) {
        $uid = $request->get('uid');
        $paytype = $request->get('pay_type');

        $shopUserStatus = $this->accountService->getUserStatus($uid);
        $recode = ShopUserStatus::STATUS_NONE;
        if($shopUserStatus -> open_status == ShopUserStatus::STATUS_SUCCESS) {
            $recode = ShopUserStatus::STATUS_SUCCESS;
        }

        return ApiResponse(['recode' => $recode]);

    }

    public function unline(Request $request) {
        $uid = $request->get("uid");
        $name = $request->get("name");
        $num = $request->get("num");

        if(empty($name) || empty($num)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $payUnlineData = $this->accountService->getPayUnlineByUid($uid);
        Log::info("线下录入当前用户：".json_encode($payUnlineData) );
        if(!empty($payUnlineData) ){
            $res = [
              'recode'=> 1001
            ];
            $msg = "对不起，您已有审核的单据";
            return ApiResponse($res, $msg);
        }

        $uniqueData = $this->accountService->getPayUnlineByNum($num);
        Log::info("线下录入当前订单是否存在：".json_encode($payUnlineData) );
        if(!empty($uniqueData) ){
            throw new AixinException("对不起，您的单据号已存在");
        }

        $addNewData = [
            'uid'   =>  $uid,
            'name'  =>  $name,
            'num'   =>  $num
        ];

        $id = $this->accountService->addPayUnline($addNewData);
        $isOk = $this->accountService->setUserStatus($uid, ShopUserStatus::CARD_PAY_STATUS, ShopUserStatus::STATUS_SUCCESS);

        if(empty($id)) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        return ApiResponse();

    }

    public function vipsure(Request $request){
        $uid = $request->get("uid");
        $shopUserStatus = $this->accountService->getUserStatus($uid);
        Log::info("激活前:".$shopUserStatus."=》{$uid}:激活用户vip操作" );
        $isOk = $this->accountService->setUserStatus($uid, ShopUserStatus::OPEN_STATUS, ShopUserStatus::STATUS_SUCCESS);
        return ApiResponse();
    }

    public function paystatus(Request $request) {
        $uid = $request->get("uid");
        $accountInfo = $this->accountService->userinfo($uid);
        Log::info("支付密码状态信息".json_encode($accountInfo));
        $recode = 1; //默认是有密码
        $token = "";
        //无密码
        if( empty($accountInfo->pay_passwd) ){

            $recode   = 0;
            $cacheKey = UID_PAY_PWD_CACHE_TOKEN_KEY.$uid;
            $token    = md5($uid);
            $isSet = Redis::set($cacheKey, $token);
            Redis::expire($cacheKey, UID_PAY_PWD_CACHE_TOKEN_TIME);
            Log::info("用户生成初始密码的token:".$token);
        }

        $data = [
            'recode' => $recode,
            'pwd_token'=> $token
        ];

        return ApiResponse($data);

    }


    public function check_paypwd(Request $request) {
        $uid = $request->get("uid");
        $passwd = $request->get("passwd");
        if(empty($passwd)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $accountInfo = $this->accountService->userinfo($uid);

        Log::info("用户当前密码：".$accountInfo->pay_passwd."用户输入密码：".md5($passwd) );
        //无密码
        if( $accountInfo->pay_passwd != md5($passwd) ){
            throw new AixinException(AixinExceptionCode::COMMON_FAILD, "密码错误！");
        }

        $cacheKey = UID_PAY_PWD_CACHE_TOKEN_KEY.$uid;
        $token    = md5($uid);
        $isSet = Redis::set($cacheKey, $token);
        Redis::expire($cacheKey, UID_PAY_PWD_CACHE_TOKEN_TIME);
        Log::info("用户生成重置密码的token:".$token);

        $data = [
            'pwd_token'=> $token
        ];

        return ApiResponse($data);

    }

    public function submitpwd(Request $request) {
        $uid = $request->get("uid");
        $passwd = $request->get("passwd");
        $pwdToken = $request->get("pwd_token");

        if(empty($passwd)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $cacheKey = UID_PAY_PWD_CACHE_TOKEN_KEY.$uid;
        $token    = md5($uid);
        $cacheToken = Redis::get($cacheKey);
        Log::info("获取redis缓存token：".json_encode($cacheToken));

        if(empty($cacheToken) || $cacheToken != $pwdToken) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD, "token失效或者过期！");
        }

        $accountInfo = $this->accountService->userinfo($uid);
        Log::info("当前密码：".json_encode($cacheToken));
        $isChange = $this->accountService->changePayPasswd($uid, $passwd);
        if(empty($isChange)) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD, "修改支付密码失败！");
        }
        //销毁修改密码的token
        Redis::del($cacheKey);


        return ApiResponse();

    }



}
